package hongcaosp.app.android.user.collect;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.comment.LoadMoreHandler;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.user.FreshInterface;
import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.R;
import hongcaosp.app.android.modle.bean.IVideo;
import hongcaosp.app.android.user.TotalCallBack;
import hongcaosp.app.android.video.player.VideosPlayActivity;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.view.OnRecycleItemClickListener;
import xlj.lib.android.base.view.ProxyAdapter;

public class GridVideoFragment extends BaseFragment implements IListView<IVideo>,FreshInterface {

    private RecyclerView recyclerView;
    private BaseAdapter<IVideo> favoriteAdapter;
    private CollectPresenter favoritePresenter;
    private TotalCallBack totalCallBack;
    private LoadMoreHandler loadMoreHandler;

    private int type;
    private long userId;

    public void setTotalCallBack(TotalCallBack totalCallBack) {
        this.totalCallBack = totalCallBack;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.recycler_view);

       final OnRecycleItemClickListener onRecycleItemClickListener = new OnRecycleItemClickListener() {
            @Override
            public void onItemClick(int position) {
                VideosPlayActivity.toPlayVideos(getContext(),type, (ArrayList<IVideo>) favoriteAdapter.getData(),
                        favoritePresenter.getNumber(),favoritePresenter.getUserId(),position);
            }

            @Override
            public void onItemLongClick(int position) {

            }
        };
        favoriteAdapter = new BaseAdapter<IVideo>() {
            @NonNull
            @Override
            public BaseHolder<IVideo> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                if (type == 1) {
                    return new CollectHolder(getContext(), LayoutInflater.from(getContext()).inflate(R.layout.item_product, parent, false), type,
                            onRecycleItemClickListener);
                }
                return new CollectHolder(getContext(), LayoutInflater.from(getContext()).inflate(R.layout.item_collect, parent, false), type,
                        onRecycleItemClickListener);
            }
        };
        ProxyAdapter proxyAdapter = new ProxyAdapter(favoriteAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3,LinearLayout.VERTICAL,false));
        recyclerView.setAdapter(proxyAdapter);

        favoritePresenter = new CollectPresenter(this, type, userId);
        favoritePresenter.freshData(totalCallBack);

        recyclerView.addOnScrollListener(loadMoreHandler = new LoadMoreHandler((LinearLayoutManager) recyclerView.getLayoutManager()) {
            @Override
            public void loadMore() {
                favoritePresenter.loadMore();
            }
        });
    }

    @Override
    public int getLayoutRes() {
        return R.layout.view_recycle;
    }

    @Override
    public void freshList(List<IVideo> data, boolean canLoadMore) {
        Log.e("GridVideo","freshList->" + canLoadMore);
        favoriteAdapter.setData(data);
        favoriteAdapter.notifyDataSetChanged();
        if (loadMoreHandler != null) {
            loadMoreHandler.loadMoreFinish(canLoadMore);
        }
    }

    @Override
    public void addList(List<IVideo> data, boolean canLoadMore) {
        favoriteAdapter.addData(data);
        favoriteAdapter.notifyDataSetChanged();
        if (loadMoreHandler != null) {
            loadMoreHandler.loadMoreFinish(canLoadMore);
        }
    }

    @Override
    public void fresh() {
//        if (favoritePresenter != null) {
//            favoritePresenter.freshData(totalCallBack);
//        }
    }

    @Override
    public void onShow() {
        super.onShow();
        if (favoritePresenter != null) {
            favoritePresenter.freshData(totalCallBack);
        }
    }
}
